package com.yunbiao.ybbusstopplate.refactoring.ui.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.yunbiao.ybbusstopplate.R;
import com.yunbiao.ybbusstopplate.databinding.ItemNodeSiteDownBinding;
import com.yunbiao.ybbusstopplate.databinding.ItemNodeSiteUpBinding;
import com.yunbiao.ybbusstopplate.refactoring.Config;
import com.yunbiao.ybbusstopplate.refactoring.ui.dalian.ZHIDAParser;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DownSiteAdapter extends RecyclerView.Adapter<DownSiteAdapter.Holder> {

    private final List<ZHIDAParser.Site> dataList;

    public DownSiteAdapter(ZHIDAParser.Site[] sites) {
        this.dataList = new ArrayList<>(Arrays.asList(sites));
    }

    private int currSiteIndex = 0;
    public void setCurrPosition(int currSiteIndex){
        this.currSiteIndex = currSiteIndex;
        notifyItemRangeChanged(0,dataList.size(),new Object());
    }

    public int getPositionOfIndex(int siteIndex){
        int position = 0;
        for (int i = 0; i < dataList.size(); i++) {
            ZHIDAParser.Site site = dataList.get(i);
            if(site.getIndex() == siteIndex){
                position = i;
                break;
            }
        }
        return position;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_node_site_down,viewGroup,false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position, @NonNull List<Object> payloads) {
        ZHIDAParser.Site site = dataList.get(position);
        if(payloads.isEmpty()){
            holder.binding.tvSite.setText(site.getChName());

            if(position == 0){
                holder.binding.vLeftLine.setVisibility(View.VISIBLE);
                holder.binding.vCenterDot.setVisibility(View.VISIBLE);
                holder.binding.vRightLine.setVisibility(View.INVISIBLE);
                holder.binding.vCenterDot.setBackgroundResource(R.drawable.shape_site_dot_start_end);
            } else if(position == getItemCount() - 1){
                holder.binding.vLeftLine.setVisibility(View.INVISIBLE);
                holder.binding.vCenterDot.setVisibility(View.VISIBLE);
                holder.binding.vRightLine.setVisibility(View.VISIBLE);
                holder.binding.vCenterDot.setBackgroundResource(R.drawable.shape_site_dot_start_end);
            } else {
                holder.binding.vLeftLine.setVisibility(View.VISIBLE);
//                if(Config.getAgreement().equals(Config.Agreement.BASHITONG)){
//                    holder.binding.vCenterDot.setVisibility(View.VISIBLE);
//                } else {
//                    holder.binding.vCenterDot.setVisibility(View.INVISIBLE);
//                }
                holder.binding.vCenterDot.setBackgroundResource(R.drawable.shape_site_dot);
                holder.binding.vRightLine.setVisibility(View.VISIBLE);
            }
        }

        if(site.getIndex() < currSiteIndex){
            holder.binding.vLeftLine.setBackgroundColor(Color.GRAY);
            holder.binding.vRightLine.setBackgroundColor(Color.GRAY);

            holder.binding.tvSite.setTextColor(Color.GRAY);
        } else if(site.getIndex() > currSiteIndex){
            if(Config.getAgreement().equals(Config.Agreement.BASHITONG)){
                holder.binding.vLeftLine.setBackgroundResource(android.R.color.holo_green_dark);
                holder.binding.vRightLine.setBackgroundResource(android.R.color.holo_green_dark);
            } else {
                holder.binding.vLeftLine.setBackgroundColor(Color.WHITE);
                holder.binding.vRightLine.setBackgroundColor(Color.WHITE);
            }

            holder.binding.tvSite.setTextColor(Color.BLACK);
        } else {
            holder.binding.vRightLine.setBackgroundColor(Color.GRAY);
            if(Config.getAgreement().equals(Config.Agreement.BASHITONG)){
                holder.binding.vLeftLine.setBackgroundResource(android.R.color.holo_green_dark);
                holder.binding.tvSite.setTextColor(holder.binding.tvSite.getContext().getResources().getColor(android.R.color.holo_green_dark));
            } else {
                holder.binding.vLeftLine.setBackgroundColor(Color.WHITE);
                holder.binding.tvSite.setTextColor(Color.WHITE);
            }
        }
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int i) {

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {

        private final ItemNodeSiteDownBinding binding;

        public Holder(@NonNull View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
        }
    }
}
